
package com.jl.js.core;

import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.WrapFactory;

/**
 * Extend Java to JavaScript wrapper to allow proper conversion of byte[] to ByteString instances.
 * 
 * This wrapper also disables wrapping of primitive Java classes String, Number, Character and Boolean, making
 * instances of these classes available as JavaScript objects.
 * 
 * @author Andreas Schwier (info@cardcontact.de)
 */
public class JsWrapFactory extends WrapFactory {

	/**
	 * Creates the wrapper
	 */
	public JsWrapFactory() {
		super();
		setJavaPrimitiveWrap(false);
	}
	
	/**
	 * Wrap instances of byte[] into ByteString objects
	 */
	@Override
	public Object wrap(Context cx, Scriptable scope, Object obj, Class<?> staticType) {

		if (obj instanceof byte[]) {
			//return new ByteArray((byte[])obj);
			return ByteArray.newInstance(scope, (byte[])obj);
		}
		return super.wrap(cx, scope, obj, staticType);
	}
}
